package com.ahdms.es.gm.asn1.cipher;

import org.bouncycastle.asn1.*;

public class SM2EnvelopedKey extends ASN1Object {

    private ASN1Integer asyAlgId;
    private ASN1Integer syAlgId;
    private SM2Cipher encSyKey;
    private SM2PublicKey publicKey;
    private DERBitString encPrivateKey;


    public static SM2EnvelopedKey getInstance(Object obj) {
        if (obj instanceof SM2EnvelopedKey) {
            return (SM2EnvelopedKey) obj;
        }
        if (obj != null) {
            return new SM2EnvelopedKey(ASN1Sequence.getInstance(obj));
        }

        return null;
    }

    protected SM2EnvelopedKey(ASN1Sequence seq) {
        this.asyAlgId = ASN1Integer.getInstance(seq.getObjectAt(0));
        this.syAlgId = ASN1Integer.getInstance(seq.getObjectAt(1));
        this.encSyKey = SM2Cipher.getInstance(seq.getObjectAt(2));
        this.publicKey = SM2PublicKey.getInstance(seq.getObjectAt(3));
        this.encPrivateKey = DERBitString.getInstance(seq.getObjectAt(4));
    }

    public SM2EnvelopedKey(
            ASN1Integer asyAlgId,
            ASN1Integer syAlgId,
            SM2Cipher encSyKey,
            SM2PublicKey publicKey,
            DERBitString encPrivateKey) {
        this.asyAlgId = asyAlgId;
        this.syAlgId = syAlgId;
        this.encSyKey = encSyKey;
        this.publicKey = publicKey;
        this.encPrivateKey = encPrivateKey;
    }

    public ASN1Integer getAsyAlgId() {
        return asyAlgId;
    }

    public ASN1Integer getSyAlgId() {
        return syAlgId;
    }

    public SM2Cipher getEncSyKey() {
        return encSyKey;
    }

    public SM2PublicKey getPublicKey() {
        return publicKey;
    }

    public DERBitString getEncPrivateKey() {
        return encPrivateKey;
    }

    @Override
    public ASN1Primitive toASN1Primitive() {
        ASN1EncodableVector seq = new ASN1EncodableVector();
        seq.add(asyAlgId);
        seq.add(syAlgId);
        seq.add(encSyKey);
        seq.add(publicKey);
        seq.add(encPrivateKey);
        return new DERSequence(seq);
    }

}
